package com.leetcode.first50;

public class Leetcode0012 {


    public static void main(String[] args) {
        Leetcode0012 leetcode0012 = new Leetcode0012();

        System.out.println(leetcode0012.intToRoman(3));
        System.out.println(leetcode0012.intToRoman(4));
        System.out.println(leetcode0012.intToRoman(9));
        System.out.println(leetcode0012.intToRoman(10));
        System.out.println(leetcode0012.intToRoman(58));
        System.out.println(leetcode0012.intToRoman(1994));


    }


    public String intToRoman(int num) {
        StringBuilder sb = new StringBuilder();
        int va = num / 1000;
        num = num % 1000;
        while (va > 0) {
            sb.append("M");
            va--;
        }
        va = num / 100;
        num = num % 100;
        if (va > 0) {
            if (va == 9) {
                sb.append("CM");
            } else if (va == 4) {
                sb.append("CD");
            } else {
                String tile = "";
                int tmp = va % 5;
                while (tmp > 0) {
                    tile += "C";
                    tmp--;
                }
                sb.append(va >= 5 ? "D" : "").append(tile);
            }
        }

        va = num / 10;
        num = num % 10;
        if (va > 0) {
            if (va == 9) {
                sb.append("XC");
            } else if (va == 4) {
                sb.append("XL");
            } else {
                String tile = "";
                int tmp = va % 5;
                while (tmp > 0) {
                    tile += "X";
                    tmp--;
                }
                sb.append(va >= 5 ? "L" : "").append(tile);
            }
        }

        va = num;
        if (va > 0) {
            if (va == 9) {
                sb.append("IX");
            } else if (va == 4) {
                sb.append("IV");
            } else {
                String tile = "";
                int tmp = va % 5;
                while (tmp > 0) {
                    tile += "I";
                    tmp--;
                }
                sb.append(va >= 5 ? "V" : "").append(tile);
            }
        }


        return sb.toString();
    }
}
